﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyDoiXe.lop_doi_tuong;
using QuanLyDoiXe.Lop_du_lieu;
using System.IO;
using BarcodeLib;

namespace QuanLyDoiXe
{
    public partial class frmchitiethoadon : Form
    {
        SqlConnection conn = new SqlConnection("Data Source=ACER;Initial Catalog=quanlydoixevinamilk;Integrated Security=True");
        public frmchitiethoadon()
        {
            InitializeComponent();
        }
        #region 1. Thoát & nhập lại
        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnnhaplai_Click(object sender, EventArgs e)
        {
            txtMHD.Text = "";
            txtdiachi.Text = "";
            txthanghoa.Text = "";
            txtkhachhang.Text = "";
            txtsoluong.Text = "";
            cbtrangthai.ResetText();
            dtpngaygiao.ResetText();
            dtpngaylap.ResetText();
            txtxegiao.Text = "";
        }
        #endregion
        #region 2. hiện CTHD
        ListViewItem item;
        private void HienThiHD()
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("Select * from hoadon,xecongty, chitiethoadon,hanghoa,khachhang,trangthai where  hoadon.Mxe=xecongty.Mxe and hoadon.MCTHD=chitiethoadon.MCTHD and chitiethoadon.Mhanghoa=hanghoa.Mhanghoa and chitiethoadon.MKH=khachhang.MKH and chitiethoadon.Mtrangthai=trangthai.Mtrangthai ", conn);
                SqlDataReader sqlDA = cmd.ExecuteReader();
                int i = 0;
                lvchitiethoadon.View = View.Details;
                lvchitiethoadon.FullRowSelect = true;
                while (sqlDA.Read())
                {
                    i++;
                    item = new ListViewItem(i.ToString());
                    item.SubItems.Add(sqlDA["MHD"].ToString());
                    item.SubItems.Add(sqlDA["TenKH"].ToString());
                    item.SubItems.Add(sqlDA["Diachi"].ToString());
                    item.SubItems.Add(sqlDA["Tenhang"].ToString());
                    item.SubItems.Add(sqlDA["Soluong"].ToString());
                    item.SubItems.Add(sqlDA["Trangthai"].ToString());
                    item.SubItems.Add(sqlDA["Mxe"].ToString());
                    item.SubItems.Add(sqlDA["Ngaygiaohang"].ToString());
                    item.SubItems.Add(sqlDA["Ngaylaphoadon"].ToString());
                    lvchitiethoadon.Items.Add(item);
                }
                cmd.Dispose();
                sqlDA.Close();
                conn.Close();
            }
            catch
            {
            }
        }

        private void lvchitiethoadon_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem item = e.Item;
            txtMHD.Text = item.SubItems[1].Text;
            txtkhachhang.Text = item.SubItems[2].Text;
            txtdiachi.Text = item.SubItems[3].Text;
            txthanghoa.Text = item.SubItems[4].Text;
            txtsoluong.Text = item.SubItems[5].Text;
            cbtrangthai.Text = item.SubItems[6].Text;
            txtxegiao.Text = item.SubItems[7].Text;
            dtpngaygiao.Text = item.SubItems[8].Text;
            dtpngaylap.Text = item.SubItems[9].Text;
        }
        string MHD, TenKH, Tenhang, Trangthai, Diachi,Mxe;
        int Soluong;
        DateTime Ngaygiaohang, Ngaylaphoadon;
        private void lvchitiethoadon_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lvchitiethoadon.Items.Count; i++)
            {
                if (lvchitiethoadon.Items[i].Selected)
                {
                    MHD = item.SubItems[1].Text;
                    TenKH = item.SubItems[2].Text;
                    Diachi = item.SubItems[3].Text;
                    Tenhang = item.SubItems[4].Text;
                    Soluong = Convert.ToInt32(item.SubItems[5].Text);
                    Trangthai = item.SubItems[6].Text;
                    Mxe = item.SubItems[7].Text;
                    Ngaygiaohang = Convert.ToDateTime(item.SubItems[8].Text);
                    Ngaylaphoadon = Convert.ToDateTime(item.SubItems[9].Text);
                }
            }
        }

        private void frmchitiethoadon_Load(object sender, EventArgs e)
        {
            HienThiHD();
        }
        #endregion
       /* #region 3. Cập Nhập
        private void btncapnhap_Click(object sender, EventArgs e)
        {
            string strMaKH = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select MKH from khachhang where TenKH=N\'" + txtkhachhang.Text + "\'");
            while (dr.Read())
            {
                strMaKH = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();

            string strhanghoa = "";
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            SqlDataReader dr1 = dl1.lay_reader("select Mhanghoa from hanghoa where Tenhang=N\'" + txthanghoa.Text + "\'");
            while (dr1.Read())
            {
                strhanghoa = dr1.GetString(0);
            }
            dr1.Close(); dr1.Dispose();
            string strtrangthai = "";
            Lop_lay_du_lieu dl2 = new Lop_lay_du_lieu();
            SqlDataReader dr2 = dl2.lay_reader("select Mtrangthai from trangthai where Trangthai=N\'" + cbtrangthai.Text + "\'");
            while (dr2.Read())
            {
                strtrangthai = dr2.GetString(0);
            }
            dr2.Close(); dr2.Dispose();
            string strMxe = "";
            Lop_lay_du_lieu dl3 = new Lop_lay_du_lieu();
            SqlDataReader dr3 = dl3.lay_reader("select Mxe from xecongty where Mxe=N\'" + txtxegiao.Text + "\'");
            while (dr3.Read())
            {
                strMxe = dr3.GetString(0);
            }
            dr3.Close(); dr3.Dispose();
            string strCTHD = "";
            Lop_lay_du_lieu dl4 = new Lop_lay_du_lieu();
            SqlDataReader dr4 = dl4.lay_reader("select MCTHD from chitiethoadon where MCTHD=N\'" + t + "\'");
            while (dr3.Read())
            {
                strMxe = dr3.GetString(0);
            }
            dr3.Close(); dr3.Dispose();
            lop_ket_noi.huyketnoi();
            if (txtMHD.Text != "" && txtkhachhang.Text != "" && txthanghoa.Text != "" && cbtrangthai.Text != "" && txtdiachi.Text != "" && txtsoluong.Text != "" && txtxegiao.Text != "" && dtpngaygiao.Text != "" && dtpngaylap.Text != "")
            {
               // lop_hoadon hoadon = new lop_hoadon(txtMHD,);
                lop_chitiethoadon CTHD = new lop_chitiethoadon(txtMHD.Text,strMaKH,strhanghoa,strtrangthai,txtdiachi.Text,Convert.ToInt32(txtsoluong.Text));
                if (CTHD.capnhat())
                    MessageBox.Show("Quá trình cập nhật CTHĐ thành công!");
                else
                    MessageBox.Show("Quá trình cập nhật CTHĐ thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ thông tin CTHĐ! ");
            lvchitiethoadon.Items.Clear();
            HienThiHD();
        }
        #endregion

        private void btnthem_Click(object sender, EventArgs e)
        {
            string strMahanghoa = "";
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select Mhanghoa from hanghoa where Tenhang=N\'" + txthanghoa.Text + "\'");
            while (dr.Read())
            {
                strMahanghoa = dr.GetString(0);
            }
            dr.Close(); dr.Dispose();

            string strkhachhang = "";
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            SqlDataReader dr1 = dl1.lay_reader("select MKH from khachhang where TenKH=N\'" + txtkhachhang.Text + "\'");
            while (dr1.Read())
            {
               strkhachhang = dr1.GetString(0);
            }
            dr1.Close(); dr1.Dispose();
            string strtrangthai = "";
            Lop_lay_du_lieu dl2 = new Lop_lay_du_lieu();
            SqlDataReader dr2 = dl2.lay_reader("select Mtrangthai from trangthai where Trangthai=N\'" + cbtrangthai.Text + "\'");
            while (dr2.Read())
            {
                strtrangthai = dr2.GetString(0);
            }
            dr2.Close(); dr2.Dispose();
            
            lop_ket_noi.huyketnoi();

            txtMHD.Enabled = true;
            if (txtMHD.Text != "" && txtkhachhang.Text != "" && txtdiachi.Text != "" && txthanghoa.Text != "" && txtsoluong.Text != "" && cbtrangthai.Text != "" )
            {
                lop_chitiethoadon CTHD = new lop_chitiethoadon(txtMHD.Text,strkhachhang,strMahanghoa,strtrangthai,txtdiachi.Text,Convert.ToInt32(txtsoluong.Text));
                
                if (CTHD.taomoi())
                    MessageBox.Show("Quá trình tạo mới CTHD thành công!");
                else
                    MessageBox.Show("Quá trình tạo mới CTHD thất bại!");
            }
            else
                MessageBox.Show("Bạn hãy nhập đầy đủ hông tin CTHD! ");
            lvchitiethoadon.Items.Clear();
            HienThiHD();
        }

        string MaCTHD(string ma)
        {
            Lop_lay_du_lieu dl1 = new Lop_lay_du_lieu();
            string tam = "";
            SqlDataReader dr1 = dl1.lay_reader("select MCTHD from chitiethoadon");
            while (dr1.Read())
                tam = dr1[0].ToString();
            lop_ket_noi.huyketnoi();
            int i = 0;
            i = int.Parse(tam.Substring(2, tam.Length - 2));
            i++;
            if (i < 10) return "CT00" + i.ToString();
            else
                if (i < 100) return "CT0" + i.ToString();
                else return "CT" + i.ToString();
        }
        private void btnkhoitao_Click(object sender, EventArgs e)
        {
            Lop_lay_du_lieu dl = new Lop_lay_du_lieu();
            SqlDataReader dr = dl.lay_reader("select MCTHD from chitiethoadon");
            string strtam = "";
            while (dr.Read())
                strtam = dr[0].ToString();
            lop_ket_noi.huyketnoi();
            if (strtam == "") txtMHD.Text = "00000";
            else txtMHD.Text = MaCTHD(strtam);
        }*/
    }
}
